//
//  HoDialogView.m
//  HOrangiOS
//
//  Created by YOrange on 2018/5/25.
//  Copyright © 2018年 YOrange. All rights reserved.
//

//获取屏幕的高度
#define KHeight [[UIScreen mainScreen] bounds].size.height
//获取屏幕的宽度
#define KWidth [[UIScreen mainScreen] bounds].size.width

#import "HoDialogView.h"
@interface HoDialogView ()


@property (strong, nonatomic) UIView * blackView;

@property (assign, nonatomic) CGRect size;

@property (assign, nonatomic) CGRect bottomFrame;

@property (strong, nonatomic) UIView *bottomView;

@end
@implementation HoDialogView{
    int xHigh;
}

-(instancetype)initWithDialogView:(UIView *)dialogView{
    if (self = [super init]) {
        [self configUI:dialogView];
        self.type = MiddleShow;
    }
    return self;
}

-(instancetype)initWithDialogView:(UIView *)dialogView animationType:(AnimationShowType)type{
    if (self = [super init]) {
        self.type = type;
        [self configUI:dialogView];
        
    }
    return self;
}


-(void)configUI:(UIView *)di{
    self.isHidden = YES;
    self.bgView = [[UIView alloc]init];
    self.bgView.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tapRecognizerWeibo=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBgView)];
    [self.bgView addGestureRecognizer:tapRecognizerWeibo];
    
    if (IPHONE_X_TYPE) {
       xHigh = 13;
   }else{
       xHigh = 0;
   }
    self.bgView.alpha = 0.0;
    [self addSubview:self.bgView];
    _dialog = di;
    self.size = di.frame;
    if (_type == BottomShow) {
        UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
        if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
           interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            _dialog.frame = CGRectMake(44, KHeight, KWidth - 44 - 34, self.size.size.height);
            _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
            _bottomView.backgroundColor = [UIColor whiteColor];
        }else{
            _dialog.frame = CGRectMake(0, KHeight, KWidth, self.size.size.height);
            _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - xHigh, SCREEN_WIDTH, xHigh)];
            _bottomView.backgroundColor = [UIColor whiteColor];
        }
        
        [self addSubview:_bottomView];
        _bottomView.hidden = YES;
    }
    //NSLog(@"🍺🍺🍺%@",_dialog);

   
    [self addSubview:_dialog];
}


-(void)layoutSubviews{
    self.bgView.frame = CGRectMake(0, 0,KWidth , KHeight);
    self.frame = CGRectMake(0, 0,KWidth , KHeight);
    if(self.type == MiddleShow){
        self.dialog.frame = self.size;
        self.dialog.center = CGPointMake(KWidth / 2.0, KHeight / 2.0);
    }else{
        self.dialog.frame = _bottomFrame ;
;
    }
    //NSLog(@"%@",NSStringFromCGSize(self.dialog.frame.size));

}

-(void)show:(UIView *)parentView{
    self.isHidden = NO;
    if ([self.window.rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
        // 禁用返回手势
        if ([nav respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            nav.interactivePopGestureRecognizer.enabled = NO;
        }
        
    }
    
    if (parentView == nil) {
        [[[UIApplication sharedApplication].delegate window] addSubview:self];
    }else{
        [parentView addSubview:self];
        CGRect frame = _dialog.frame;
        frame.origin.y = parentView.size.height;
        _dialog.frame = frame;
    }
    //默认冲中间弹出的
    if(self.type == MiddleShow){
        self.dialog.transform = CGAffineTransformMakeScale(1.2, 1.2);
        [UIView animateWithDuration:0.2 animations:^{
            self.dialog.transform = CGAffineTransformMakeScale(1.0, 1.0);
            self.bgView.alpha = 0.4;
        } completion:^(BOOL finished) {
            //避免多次弹出之后alpha值为0
            //self.viewParent.alpha = 1;
        }];
    //从底部弹出的
    }else if (self.type == BottomShow){
        [UIView animateWithDuration:0.2 animations:^{
            CGRect frame = _dialog.frame;
            frame.origin.y -= self.size.size.height;
            frame.origin.y -= xHigh;
            _dialog.frame = frame;
            self.bgView.alpha = 0.4;
            _bottomFrame = frame;
            _bottomView.hidden = NO;
        } completion:^(BOOL finished) {
            //避免多次弹出之后alpha值为0
            //self.viewParent.alpha = 1;
            
        }];
    }
    
}


-(void)showSafeArea:(UIView *)parentView{
    
    if ([self.window.rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
        // 禁用返回手势
        if ([nav respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            nav.interactivePopGestureRecognizer.enabled = NO;
        }
        
    }
    if (parentView == nil) {
        [[[UIApplication sharedApplication].delegate window] addSubview:self];
    }else{
        [parentView addSubview:self];
    }
    //默认冲中间弹出的
    if(self.type == MiddleShow){
        self.dialog.transform = CGAffineTransformMakeScale(1.2, 1.2);
        [UIView animateWithDuration:0.2 animations:^{
            self.dialog.transform = CGAffineTransformMakeScale(1.0, 1.0);
            self.bgView.alpha = 0.4;
        } completion:^(BOOL finished) {
            //避免多次弹出之后alpha值为0
            //self.viewParent.alpha = 1;
        }];
    //从底部弹出的
    }else if (self.type == BottomShow){
        [UIView animateWithDuration:0.2 animations:^{
            CGRect frame = _dialog.frame;
            frame.origin.y -= self.size.size.height;
            frame.origin.y -= xHigh;
            _dialog.frame = frame;
            self.bgView.alpha = 0.4;
            _bottomFrame = frame;
            _bottomView.hidden = NO;
        } completion:^(BOOL finished) {
            //避免多次弹出之后alpha值为0
            //self.viewParent.alpha = 1;
            
        }];
    }
    
}


-(void)bottomMove:(CGFloat)distance{
    if (self.type == BottomShow){
        [UIView animateWithDuration:0.2 animations:^{
            CGRect frame = self.dialog.frame;
            frame.origin.y -= distance;
            self.dialog.frame = frame;
        } completion:^(BOOL finished) {
            //避免多次弹出之后alpha值为0
            //self.viewParent.alpha = 1;
            
        }];
    }
}


-(void)dismiss{
    self.isHidden = YES;
    if ([self.window.rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
        // 返回手势
        if ([nav respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            nav.interactivePopGestureRecognizer.enabled = YES;
        }
        
    }
    //self.dialog.transform = CGAffineTransformMakeScale(1.0, 1.0);
    if (_type == MiddleShow) {
        [UIView animateWithDuration:0.2 animations:^{
            //self.dialog.transform = CGAffineTransformMakeScale(0.3, 0.3);
            self.dialog.alpha = 0.0;
            self.bgView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            self.dialog.alpha = 1.0;
            self.bgView.alpha = 0.4;

        } ];
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            CGRect frame = _dialog.frame;
            frame.origin.y += self.size.size.height;
            frame.origin.y += xHigh;
            _dialog.frame = frame;
            self.bgView.alpha = 0.0;
            _bottomFrame = frame;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            _bottomView.hidden = YES;
            //避免多次弹出之后alpha值为0
            //self.viewParent.alpha = 1;
        }];
    }
    
}

//-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    if (!self.clickEmptyNotDismiss) {
//        [self dismiss];
//    }
//
//}

-(void)clickBgView{
    [self endEditing:YES];
    if (!self.clickEmptyNotDismiss) {
        [self dismiss];
        if(_clickEmtypBlock){
            _clickEmtypBlock();
        }
    }
}


+(HoDialogView *)showAlertView:(UIView *)vi{
    HoDialogView *alert = [[HoDialogView alloc]initWithDialogView:vi animationType:MiddleShow];
    [alert show:nil];
    return alert;
}

+(HoDialogView *)showAlertView:(UIView *)vi type:(AnimationShowType)type{
    HoDialogView *alert = [[HoDialogView alloc]initWithDialogView:vi animationType:type];
    [alert show:nil];
    return alert;
}

+(HoDialogView *)showAlertXibView:(NSString *)xib{
    return [HoDialogView showAlertXibView:xib type:MiddleShow];
}


+(HoDialogView *)showAlertXibView:(NSString *)xib type:(AnimationShowType)type{
    UIView *vi = [[[NSBundle mainBundle]loadNibNamed:xib owner:self options:nil] lastObject];
//    assert(vi,@"没有xib视图");
    NSAssert(vi, @"请仔细检查是否创建有xib视图");
    HoDialogView *alert = [[HoDialogView alloc]initWithDialogView:vi animationType:type];
    [alert show:nil];
    return alert;
}


-(void)dealloc{
//    NSLog(@"弹框释放！");
}

@end

